package com.erplan.swt.app.gx;

import java.sql.Connection;

import net.miginfocom.swt.MigLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public class Gx {

	//=====================================================================
	//Global declarators
	//=====================================================================
	public static String toolText = "Gx Generic SWT Application";
	public static String toolTextSmall = "Gx SWT App";
	public static String toolVersion = "v0.1";

    public static Connection db = null;

	//=====================================================================
	//Global variables
	//=====================================================================
    public final static Display display = new Display();
    public final static Shell shell = new Shell(display, SWT.MIN | SWT.CLOSE | SWT.RESIZE);
    public static Menu menuBar = null;
    public static Composite clientArea = null;
    public static Composite statusBarArea = null;

	//=====================================================================
    //Begin of main() function
	//=====================================================================
    public static void main(String[] args) {
		//=====================================================================
		//Initializing database connection, variables
		//=====================================================================
		//DBConnection.get();
		//toolDateFormatSelected = (int)ManageAppData.GetIntData("tool_date_format_selected");

		//=====================================================================
	    //Displaying splash screen
	    //=====================================================================
		GxSplashScreen.showSplashScreen();

		//=====================================================================
		//Trial codes
	    //=====================================================================
    	//TestCode.executeTestCode(display, shell);

		//=====================================================================
		//Creating main window
	    //=====================================================================
		final Image iconImage = new Image(display, Gx.class.getResourceAsStream("images/icon.gif"));

		shell.setImage(iconImage);
		shell.setText(Gx.toolTextSmall);
		shell.setSize(600, 600);
		MigLayout migLayout = new MigLayout(
				"", 							// Layout Constraints
				"0 [] 0", 						// Column constraints
				"0 [grow] 0 [fill] 0"); 					// Row constraints
		shell.setLayout(migLayout);

		shell.addListener (SWT.Close, new Listener () {
			public void handleEvent (Event event) {
				MessageBox messageBox = new MessageBox (shell, SWT.APPLICATION_MODAL | SWT.ICON_QUESTION | SWT.YES | SWT.NO);
				messageBox.setText(Gx.toolTextSmall);
				messageBox.setMessage("Close " + Gx.toolTextSmall + " ?");
				event.doit = messageBox.open() == SWT.YES;
			}
		});


		//=====================================================================
		//Creating client area
	    //=====================================================================
		clientArea = new Composite(shell, SWT.NONE);
		clientArea.setLayoutData("wrap, w 100%, h 98%");
		//clientArea.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

		//=====================================================================
		//Creating Status Bar
	    //=====================================================================
		GxStatusBar.createStatusBar();

	    //=====================================================================
	    //Creating Menu bar
	    //=====================================================================
		GxMenuBar.createMenuBar();

	    //=====================================================================
		//Creating Popup Menu
	    //=====================================================================
		GxPopupMenu.createPopupMenu();

	    //=====================================================================
		//Setting position of the Main Window
	    //=====================================================================
		Rectangle shellRect = shell.getBounds();
		Rectangle displayRect = display.getBounds();
	    int shellX = (displayRect.width - shellRect.width) / 2;
	    int shellY = (displayRect.height - shellRect.height) / 2;
	    shell.setLocation(shellX, shellY);
	    shell.open();

		//=====================================================================
	    //Displaying bottom tray balloon
	    //=====================================================================
	    //GxTrayNotifications.showDefault(display,shell);

		//=====================================================================
	    //Shutting down the display, application
	    //=====================================================================
	    while (!shell.isDisposed()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose();

		//=====================================================================
	    //Shutting down the database connection
	    //=====================================================================
		//DBConnection.close();
    }
}